home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / barcodes.zip / SAMPLE1.ZIP / BARVW.CPP < prev    next >
C/C++ Source or Header  |  1994-11-07  |  13KB  |  423 lines

  1. // barvw.cpp : implementation of the CBarView class
  2. //
  3.  
  4. // (c) Copyright 1994 HEX TECHNOLOGY. All rights reserved.
  5. // This sample code is provided as a example implementation
  6. // using barcodes.dll.  You may only distribute or modify this
  7. // code if you purchase a license for barcodes.dll.
  8.  
  9.  
  10. #include "stdafx.h"
  11. #include "bartest.h"
  12.  
  13. #include "bardoc.h"
  14. #include "barvw.h"  
  15. #include "barcodes.h"  
  16. #include "ctl3d.h"  
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CBarView
  25.  
  26. IMPLEMENT_DYNCREATE(CBarView, CFormView)
  27.  
  28. BEGIN_MESSAGE_MAP(CBarView, CFormView)
  29.     //{{AFX_MSG_MAP(CBarView)
  30.     ON_BN_CLICKED(IDC_BACKGROUND, OnBackground)
  31.     ON_BN_CLICKED(IDC_BARCOLOR, OnBarcolor)
  32.     ON_EN_KILLFOCUS(IDC_DATA, OnKillfocus)
  33.     ON_BN_CLICKED(IDC_HORIZONTAL, OnClicked)
  34.     ON_WM_DRAWITEM()
  35.     ON_CBN_SELCHANGE(IDC_STYLE, OnSelchangeStyle)
  36.     ON_EN_KILLFOCUS(IDC_FONTFACE, OnKillfocus)
  37.     ON_BN_CLICKED(IDC_BOLD, OnClicked)
  38.     ON_BN_CLICKED(IDC_CHECK, OnClicked)
  39.     ON_BN_CLICKED(IDC_ITALIC, OnClicked)
  40.     ON_CBN_SELCHANGE(IDC_RATIO, OnClicked)
  41.     ON_BN_CLICKED(IDC_SHOWTEXT, OnClicked)
  42.     ON_BN_CLICKED(IDC_SWITCH, OnClicked)
  43.     ON_EN_KILLFOCUS(IDC_TEXT, OnKillfocus)
  44.     ON_EN_KILLFOCUS(IDC_TWEAK, OnKillfocus)
  45.     ON_BN_CLICKED(IDC_VERTICAL, OnClicked)
  46.     ON_EN_KILLFOCUS(IDC_HEIGHT, OnKillfocus)
  47.     ON_EN_KILLFOCUS(IDC_WIDTH, OnKillfocus)
  48.     ON_CBN_KILLFOCUS(IDC_STYLE, OnKillfocus)
  49.     ON_BN_CLICKED(IDC_SPACER, OnClicked)
  50.     //}}AFX_MSG_MAP
  51.     // Standard printing commands
  52.     ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
  53.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
  54. END_MESSAGE_MAP()
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CBarView construction/destruction
  58.  
  59. CBarView::CBarView()
  60.     : CFormView(CBarView::IDD)
  61. {
  62.     CString settings("Settings");
  63.     //{{AFX_DATA_INIT(CBarView)
  64.     m_sData = "";
  65.     m_sText = "";
  66.     m_bBold = FALSE;
  67.     m_bCheck = FALSE;
  68.     m_sFontFace = "";
  69.     m_bHorizontal = FALSE;
  70.     m_bItalic = FALSE;
  71.     m_iRatio = 0;
  72.     m_bShowData = FALSE;
  73.     m_bSwitch = FALSE;
  74.     m_iFontAdjust = 0;
  75.     m_bVertical = FALSE;
  76.     m_sStyle = AfxGetApp()->GetProfileString("Settings","StyleName", "EAN 8,11");
  77.     m_iWidth = 0;
  78.     m_iHeight = 0;
  79.     m_bSpacer = FALSE;
  80.     //}}AFX_DATA_INIT
  81.     // TODO: add construction code here
  82.     m_bCentreBarcode = TRUE;
  83. }
  84.  
  85. CBarView::~CBarView()
  86. {   
  87.     AfxGetApp()->WriteProfileString("Settings","StyleName", m_sStyle);
  88.  
  89. }
  90.  
  91. void CBarView::DoDataExchange(CDataExchange* pDX)
  92. {
  93.     CFormView::DoDataExchange(pDX);
  94.     //{{AFX_DATA_MAP(CBarView)
  95.     DDX_Control(pDX, IDC_PREVIEW, m_Preview);
  96.     DDX_Text(pDX, IDC_DATA, m_sData);
  97.     DDX_Text(pDX, IDC_TEXT, m_sText);
  98.     DDX_Check(pDX, IDC_BOLD, m_bBold);
  99.     DDX_Check(pDX, IDC_CHECK, m_bCheck);
  100.     DDX_Text(pDX, IDC_FONTFACE, m_sFontFace);
  101.     DDX_Check(pDX, IDC_HORIZONTAL, m_bHorizontal);
  102.     DDX_Check(pDX, IDC_ITALIC, m_bItalic);
  103.     DDX_CBIndex(pDX, IDC_RATIO, m_iRatio);
  104.     DDX_Check(pDX, IDC_SHOWTEXT, m_bShowData);
  105.     DDX_Check(pDX, IDC_SWITCH, m_bSwitch);
  106.     DDX_Text(pDX, IDC_TWEAK, m_iFontAdjust);
  107.     DDX_Check(pDX, IDC_VERTICAL, m_bVertical);
  108.     DDX_CBString(pDX, IDC_STYLE, m_sStyle);
  109.     DDX_Text(pDX, IDC_WIDTH, m_iWidth);
  110.     DDX_Text(pDX, IDC_HEIGHT, m_iHeight);
  111.     DDX_Check(pDX, IDC_SPACER, m_bSpacer);
  112.     //}}AFX_DATA_MAP
  113. }
  114.  
  115. void CBarView::OnUpdate(CView*, LPARAM, CObject*)
  116. {             
  117.     CBarDoc *pDoc = GetDocument();
  118.     m_sData = pDoc->m_sData;
  119.     m_sText = pDoc->m_sText;
  120.     m_bBold = pDoc->m_bBold;
  121.     m_iFontAdjust = pDoc->m_iFontAdjust;
  122.     m_sFontFace = pDoc->m_sFontFace;
  123.     m_bItalic = pDoc->m_bItalic;
  124.     m_iRatio = pDoc->m_iRatio;
  125.     m_iStyle = pDoc->m_iStyle;
  126.     m_iWidth = pDoc->m_iWidth;
  127.     m_iHeight = pDoc->m_iHeight;
  128.     m_bCheck = (pDoc->m_iDisplayFlags & FLAG_CheckDigit) != 0;
  129.     m_bHorizontal = (pDoc->m_iDisplayFlags & FLAG_WBearerBar) != 0;
  130.     m_bShowData = (pDoc->m_iDisplayFlags & FLAG_ShowData) != 0;
  131.     m_bSwitch = (pDoc->m_iDisplayFlags & FLAG_SwitchText) != 0;
  132.     m_bVertical = (pDoc->m_iDisplayFlags & FLAG_HBearerBar) != 0; 
  133.     m_bSpacer = (pDoc->m_iDisplayFlags & FLAG_ShowSpacer) != 0;  
  134.       
  135.     UpdateData(FALSE);  // set the data into the controls   
  136.                 
  137. };
  138.  
  139. void CBarView::OnDataChange()
  140. {             
  141.     UpdateData(TRUE);  // get values from dialog
  142.     
  143.     CBarDoc *pDoc = GetDocument();
  144.     
  145.     pDoc->m_sData = m_sData;   // save to document variables
  146.     pDoc->m_sText = m_sText;
  147.     pDoc->m_bBold = m_bBold;
  148.     pDoc->m_iFontAdjust = m_iFontAdjust;
  149.     pDoc->m_bItalic = m_bItalic;
  150.     pDoc->m_iRatio = m_iRatio;
  151.     pDoc->m_iStyle = m_iStyle;
  152.     pDoc->m_sFontFace = m_sFontFace;
  153.     pDoc->m_iHeight = m_iHeight;
  154.     pDoc->m_iWidth = m_iWidth;
  155.            
  156.     pDoc->m_iDisplayFlags = 0;
  157.     pDoc->m_iDisplayFlags |= (m_bCheck ? FLAG_CheckDigit : 0);
  158.     pDoc->m_iDisplayFlags |= (m_bHorizontal ? FLAG_WBearerBar : 0);
  159.     pDoc->m_iDisplayFlags |= (m_bShowData ? FLAG_ShowData : 0);
  160.     pDoc->m_iDisplayFlags |= (m_bSwitch ? FLAG_SwitchText : 0);
  161.     pDoc->m_iDisplayFlags |= (m_bVertical  ? FLAG_HBearerBar : 0);  
  162.     pDoc->m_iDisplayFlags |= (m_bSpacer  ? FLAG_ShowSpacer : 0);  
  163.     
  164.       
  165. };
  166.  
  167.  
  168. void CBarView::OnInitialUpdate() {
  169.  
  170.    CFormView::OnInitialUpdate();
  171.  
  172.    ResizeParentToFit(FALSE);
  173.    Ctl3dSubclassDlg(m_hWnd, CTL3D_ALL); 
  174. };
  175.  
  176. /////////////////////////////////////////////////////////////////////////////
  177. // CBarView printing
  178.  
  179. BOOL CBarView::OnPreparePrinting(CPrintInfo* pInfo)
  180. {
  181.     pInfo->SetMaxPage(1);
  182.     pInfo->SetMinPage(1);
  183.     return DoPreparePrinting(pInfo);
  184. }
  185.  
  186. void CBarView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  187. {
  188.     // TODO: add extra initialization before printing
  189. }
  190.  
  191. void CBarView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  192. {
  193.     // TODO: add cleanup after printing
  194. }
  195.  
  196. void CBarView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
  197. {
  198.  
  199.     CRect rect( pInfo->m_rectDraw);
  200.     
  201.     m_bCentreBarcode = TRUE;
  202.     
  203.     DrawBarcodePrivate( pDC, rect);     
  204.  
  205.      
  206. }                          
  207.  
  208.  
  209. // DrawBarcodePrivate()
  210. // This function is a do-all function to draw the barcode on the screen
  211. // or to the printer DC... 
  212.  
  213. BOOL CBarView::DrawBarcodePrivate( CDC *pDC, CRect rect ) {
  214.  
  215.     HMETAFILE hmf;                        // holds pointer to barcode metafile 
  216.  
  217.                                         // set by call to dll->DrawBarcode
  218.       int entireWidth, entireHeight, barcodeWidth, barcodeHeight;
  219.     
  220.     int fontFlags = (m_bBold ? FONT_BOLD : 0) + ( m_bItalic ? FONT_ITALIC : 0);
  221.     
  222.     pDC->SaveDC();
  223.     
  224.     // Call to barcode.dll function with parameters
  225.       hmf = DrawBarcode(         pDC->m_hDC, 
  226.                               (int) m_iStyle,
  227.                               (LPSTR) (const char *) m_sData,
  228.                               (LPSTR) (const char *)m_sText,
  229.                                GetDocument()->m_iDisplayFlags,
  230.                                m_iRatio, 
  231.                                (LPSTR)(const char *)m_sFontFace,
  232.                                fontFlags,  
  233.                                m_iFontAdjust,
  234.                                 GetDocument()->m_cBarColor,
  235.                                 GetDocument()->m_cBackgroundColor,
  236.                                 (int far *) &entireWidth,         // dll sets these four values
  237.                                 (int far *) &entireHeight,        // on return
  238.                                 (int far *) &barcodeWidth,
  239.                                 (int far *) &barcodeHeight);
  240.                                 
  241.  
  242.      pDC->SetMapMode( MM_ANISOTROPIC);        // so we can scale the extents
  243.   
  244.     if( hmf == NULL)                        // if NULL then invalid data string
  245.         return FALSE; 
  246.  
  247.     // widthOption, heightOption
  248.     // 1 = Scaled to size in millimetres specified by m_iWidth and m_iHeight
  249.     // 0 = Scaled on a percentage basis of returned barcode size.
  250.       
  251.      int widthOption = 1, heightOption = 1;    
  252.     
  253.      // scaleWidth, scaleHeight
  254.      // Since the desired size ( m_Width and m_Height) relate to the actual
  255.      // bars of the barcode and not the space taken by the text
  256.      // we must calculate the size of the entire barcode metafile so that the
  257.      // internal barcode m_iHeight and m_iWidth are as desired.
  258.      // therefore we increase them in proportion to the fraction
  259.      // (entireW/H / barcodeW/H )
  260.      // if percentage basis ( option = 0) then calculate percentage scaling
  261.      // of the entire value eg: ( entire * ( percent / 100 ))
  262.       
  263.      float scaleWidth = 
  264.              ((widthOption == 1) ? 
  265.                    (m_iWidth * (float)(entireWidth  / (float)barcodeWidth)) 
  266.                 : (entireWidth * (m_iWidth / 100.0F))) ;
  267.        
  268.      float scaleHeight = 
  269.                ( heightOption == 1) ?
  270.                (m_iHeight * (float)(entireHeight / (float)barcodeHeight))    
  271.               : ( entireHeight * (float) ( m_iHeight / 100.0F));
  272.  
  273.      
  274.      // How many (device units) pixels do we require to fill the desired 
  275.      // scaleWidth and scaleHeight
  276.    
  277.      float barWidthPixel=  
  278.              (widthOption == 1 ) ? 
  279.                  ((float)scaleWidth /   pDC->GetDeviceCaps(HORZSIZE)) * (float)pDC->GetDeviceCaps(HORZRES)
  280.               : scaleWidth;
  281.  
  282.      float barHeightPixel= 
  283.              (heightOption == 1) ? 
  284.                  ((float)scaleHeight /   pDC->GetDeviceCaps(VERTSIZE)) * (float)pDC->GetDeviceCaps(VERTRES) 
  285.              : scaleHeight;
  286.     
  287.  
  288.     // little bit of extra code to centre barcode in window
  289.  
  290.     if( m_bCentreBarcode ) {  
  291.         int xoff, yoff;
  292.         xoff = (int) ((rect.Width() - barWidthPixel) / 2.0);
  293.         yoff = (int) ((rect.Height() - barHeightPixel) / 2.0);
  294.         if( xoff < 0)
  295.                 xoff = 0;
  296.          if( yoff < 0)
  297.                 yoff = 0;
  298.           pDC->OffsetViewportOrg( xoff, yoff);   
  299.     } 
  300.     
  301.      // HIMETRIC 1000, 1000...
  302.      pDC->SetWindowExt(1000,1000); 
  303.       
  304.      int xratio =   (int) ((barWidthPixel / (float)entireWidth) * 1000.0F);
  305.      int yratio = (int) ((barHeightPixel / (float)entireHeight) * 1000.0F);
  306.     
  307.      // xratio, yratio are 1000  + or - some value to scale up or scale down
  308.      // the size of the metafile to get desired size.    
  309.     
  310.     pDC->SetViewportExt(xratio, yratio);
  311.  
  312.     // and after all that .. we finally get to see something !!!                            
  313.     pDC->PlayMetaFile(hmf);
  314.  
  315.     // remember to always delete the Metafile
  316.     DeleteMetaFile(hmf);  
  317.  
  318.     // tidy up
  319.     pDC->RestoreDC(-1);  // for SaveDC
  320.   
  321.     return TRUE;  
  322. };
  323.  
  324.  
  325.  
  326. /////////////////////////////////////////////////////////////////////////////
  327. // CBarView diagnostics
  328.  
  329. #ifdef _DEBUG
  330. void CBarView::AssertValid() const
  331. {
  332.     CFormView::AssertValid();
  333. }
  334.  
  335. void CBarView::Dump(CDumpContext& dc) const
  336. {
  337.     CFormView::Dump(dc);
  338. }
  339.  
  340. CBarDoc* CBarView::GetDocument() // non-debug version is inline
  341. {
  342.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBarDoc)));
  343.     return (CBarDoc*)m_pDocument;
  344. }
  345. #endif //_DEBUG
  346.  
  347. /////////////////////////////////////////////////////////////////////////////
  348. // CBarView message handlers
  349.  
  350. void CBarView::OnBackground()
  351. {
  352.     CColorDialog dlg( GetDocument()->m_cBackgroundColor); 
  353.     if( dlg.DoModal() == IDOK)
  354.          GetDocument()->m_cBackgroundColor = dlg.GetColor();                                       
  355.     UpdatePreview();
  356.     
  357. }
  358.  
  359. void CBarView::OnBarcolor()  
  360. {
  361.     CColorDialog dlg( GetDocument()->m_cBarColor); 
  362.     if( dlg.DoModal() == IDOK)
  363.          GetDocument()->m_cBarColor = dlg.GetColor();                                       
  364.     UpdatePreview();    
  365.  
  366. void CBarView::UpdatePreview() {
  367.  
  368.   OnDataChange();
  369.   m_Preview.Invalidate();
  370. };
  371.  
  372. void CBarView::OnKillfocus()
  373. {
  374.        UpdatePreview();
  375. }
  376.  
  377. void CBarView::OnClicked()
  378. {
  379.     UpdatePreview();
  380.  
  381.  
  382.  
  383. // OnDrawItem
  384. // Called by Windows when barcode preview windows needs redrawing..
  385. void CBarView::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
  386. {
  387.     if( nIDCtl == IDC_PREVIEW) {
  388.     
  389.     CDC * dc = CDC::FromHandle( lpDrawItemStruct->hDC);
  390.     CRect rect(&(lpDrawItemStruct->rcItem));
  391.     
  392.     // clear the preview window first
  393.     dc->PatBlt( rect.left,rect.top, rect.right, rect.bottom, WHITENESS );
  394.     if( ! DrawBarcodePrivate( dc, rect) ) 
  395.       dc->DrawText("Invalid Data for Style", 22, rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  396.     
  397.       }
  398.      else
  399.         CFormView::OnDrawItem(nIDCtl, lpDrawItemStruct);
  400. }
  401.  
  402. void CBarView::OnSelchangeStyle()
  403. {
  404.     UpdateData(TRUE);
  405.     SaveStyle();   
  406.     UpdatePreview();
  407.  
  408.  
  409. // SaveStyle
  410. // the style combo box stores the style in the form name,styleNum
  411. // we want the styleNum part only....
  412. // ideally this combo should be an ownerdrawn box so you don't have
  413. // to display the style num within the combo box items.
  414. void CBarView::SaveStyle() {                           
  415.     // grab everything after the comma
  416.      CString index =  m_sStyle.Right( m_sStyle.GetLength() - m_sStyle.Find(',') - 1);    
  417.      // convert to integer and store in m_iStyle
  418.      sscanf((const char *) index , "%i", & m_iStyle);
  419. };
  420.